package com.plugin.manager.utll;

import com.plugin.common.LoginService;

import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;


public class PluginManager {

    private URLClassLoader loader;

    public PluginManager(Plugin plugins) throws MalformedURLException {
        init(plugins);
    }

    private void init(Plugin plugin) throws MalformedURLException {
        URL[] urls = new URL[1];
        String filePath = plugin.getJar();
        urls[0] = new URL("file:" + filePath);
        loader = new URLClassLoader(urls);
    }

    public LoginService getLoginService(String className) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
        Class<?> clazz = loader.loadClass(className);
        return (LoginService) clazz.newInstance();
    }
}
